package com.demo.ui.fragment;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;

import com.demo.bbdd.NTContract;
import com.demo.ui.adapter.PlacesAdapter;

public class PlacesListFragment extends ListFragment implements
		PlacesInterface, LoaderCallbacks<Cursor> {

	private PlacesAdapter adapter;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		adapter = new PlacesAdapter(getActivity(), null);
		
		setListAdapter(adapter);
		
		getPointsFromDb();
	}
		
	@Override
	public void getPointsFromDb() {
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(getActivity(), NTContract.PLACE.URI, null,
				null, null, NTContract.PLACE.TIME);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
		adapter.swapCursor(c);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		adapter.swapCursor(null);
	}
}
